/**
 * 获取节点的全部子节点Id
 * @param nodes 节点数据
 * @returns {*[]}
 */
import { EntityTypes } from '@/utils/orgConstants';

const findChildrenIds = (nodes) => {
	let unitIds = [];
	for (let node of nodes) {
		unitIds.push(node.id);
		if (node.children && node.children.length > 0) {
			let childNodes = findChildrenIds(node.children);
			unitIds = [...unitIds, ...childNodes];
		}
	}
	return unitIds;
};

/**
 * 获取当前节点及直接点Id
 * @param nodes 节点数据
 * @param currentId 需要查找的目标节点
 */
const findMyChildrenIds = (nodes, currentId) => {
	let units = [];
	for (let node of nodes) {
		if (node.id === currentId) {
			if (node.children && node.children.length > 0) {
				let childrenIds = findChildrenIds(node.children);
				units = [...units, ...childrenIds];
			}
			break;
		} else if (node.children && node.children.length > 0) {
			let childrenIds = findMyChildrenIds(node.children, currentId);
			if (childrenIds.length > 0) {
				return childrenIds;
			}
		}
	}
	return units;
};

/**
 * 查找当前节点的所有父节点
 * @param curentNodes 节点数据
 * @param targetId 需要查找的目标节点
 * @returns {{nodeIds: *[], success: boolean}|{nodeIds: [], success: boolean}|*|{nodeIds: [], success: boolean}}
 */
const findParents = (curentNodes, targetId) => {
	if (!curentNodes) {
		return { success: false, nodeIds: [] };
	}
	for (let node of curentNodes) {
		if (node.id === targetId) {
			// ok you find me!
			return { success: true, nodeIds: [] };
		}
		// find my children
		let result = findParents(node.children, targetId);
		if (result.success) {
			result.nodeIds.push(node.id);
			return result;
		}
		// continue
	}
	return { success: false, nodeIds: [] };
};

/**
 * 判断是否有子节点被选择
 * @param children 子节点
 * @param selectMap 被选择的map
 * @returns {boolean}
 */
const hasChildrenSelected = (children, selectMap) => {
	if (!children || children.length === 0) {
		return false;
	}
	for (let child of children) {
		if (selectMap[child.type + '|' + child.id]) {
			return true;
		}
		if (hasChildrenSelected(child.children, selectMap)) {
			return true;
		}
	}
	return false;
};

/**
 * 判断是否有子节点被选择
 * @param nodes 节点数组
 * @param selectMap 被选择的map
 * @param checked true标示返回已勾选的数据，false标示返回未勾选的
 * @returns {*[]}
 */
const findDeptChildren = (nodes, selectMap, checked) => {
	let childrenNodes = [];
	const dealDeptChildren = (currentNodes) => {
		if (!currentNodes || currentNodes.length === 0) {
			return;
		}
		for (let child of currentNodes) {
			if (child.children && child.children.length) {
				dealDeptChildren(child.children);
			}
			if (child.type !== EntityTypes.Department && child.type !== EntityTypes.BusinessDepartment) {
				continue;
			}
			if (selectMap[child.type + '|' + child.id] && checked === true) {
				childrenNodes.push(child);
			} else if (!selectMap[child.type + '|' + child.id] && checked === false) {
				childrenNodes.push(child);
			}
		}
	};
	dealDeptChildren(nodes);
	return childrenNodes;
};

/**
 * 重置树数据，删除节点无数据的children属性
*/
const resetTreeData = (treeData) =>{
	let newTreeData = JSON.parse(JSON.stringify(treeData));
	const setFunc = (data) => {
		data.map(node => {
			if (node.hasOwnProperty('children') && node.children.length === 0) {
				delete node.children;
			}
			if (node.hasOwnProperty('children') && node.children.length > 0) {
				setFunc(node.children);
			}
		});
	};
	setFunc(newTreeData);
	return newTreeData;
};

/**
 * 树节点处理工具
 */
export const TreeUtils = {
	findMyChildrenIds,
	findChildrenIds,
	findParents,
	hasChildrenSelected,
	findDeptChildren,
	resetTreeData
};
